/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddWeightUnitMeasure.java
 *
 * Created on Mar 19, 2011, 10:18:30 AM
 */

package GUI;

import Business.WeightUnitMeasure;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author tuanba_b00920
 */
public class AddWeightUnitMeasure extends javax.swing.JDialog {

    /** Creates new form AddWeightUnitMeasure */
    public AddWeightUnitMeasure(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        weight=new WeightUnitMeasure();
       LoadList();
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        bntAdd = new javax.swing.JButton();
        bntDelete = new javax.swing.JButton();
        bntCancel = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ListWeight = new javax.swing.JList();
        txtWeightUnitMeasure = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Weight Unit Measure", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        bntAdd.setText("Add");
        bntAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntAddActionPerformed(evt);
            }
        });

        bntDelete.setText("Delete");
        bntDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntDeleteActionPerformed(evt);
            }
        });

        bntCancel.setText("Cancel");
        bntCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelActionPerformed(evt);
            }
        });

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        ListWeight.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ListWeightMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(ListWeight);

        txtWeightUnitMeasure.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtWeightUnitMeasureActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                    .addComponent(txtWeightUnitMeasure, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(txtWeightUnitMeasure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bntCancel)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(bntAdd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnEdit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bntDelete, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(bntAdd)
                        .addGap(18, 18, 18)
                        .addComponent(btnEdit)
                        .addGap(18, 18, 18)
                        .addComponent(bntDelete)
                        .addGap(18, 18, 18)
                        .addComponent(bntCancel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bntCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_bntCancelActionPerformed

    private void bntAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntAddActionPerformed
        // TODO add your handling code here:
        
        if(txtWeightUnitMeasure.getText().length()==0)
        {
          JOptionPane.showMessageDialog(rootPane, "Not valid");
          txtWeightUnitMeasure.requestFocusInWindow();
          return;
        }
        else
          if(!weight.checkSymbol(txtWeightUnitMeasure.getText())){
            if(weight.Add(txtWeightUnitMeasure.getText()))
            {
               JOptionPane.showMessageDialog(rootPane, "Succes w!");
               LoadList();
           }
       }
       else
          JOptionPane.showMessageDialog(rootPane,"the Symbol is already existing in Database !");
    }//GEN-LAST:event_bntAddActionPerformed
   private void LoadList()
   {
      ListWeight.setListData(weight.getListToVector());
   }
    private void txtWeightUnitMeasureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtWeightUnitMeasureActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtWeightUnitMeasureActionPerformed

    private void bntDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntDeleteActionPerformed
        // TODO add your handling code here:
        int index=ListWeight.getSelectedIndex();
        if(index>-1)
        {
            weight.Delete(NameSelected);
            NameSelected =null;
            txtWeightUnitMeasure.setText("");
            LoadList();
         }
       else
           JOptionPane.showMessageDialog(rootPane,"Please select a Symbol on ListBox !");
    }//GEN-LAST:event_bntDeleteActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
         int index=ListWeight.getSelectedIndex();
       if(index>-1)
        {
           weight.Edit(txtWeightUnitMeasure.getText(), NameSelected);
           LoadList();
        }
      else
             JOptionPane.showMessageDialog(rootPane,"Please select a Symbol on ListBox !");
    }//GEN-LAST:event_btnEditActionPerformed

    private void ListWeightMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ListWeightMouseClicked
        // TODO add your handling code here: 
            txtWeightUnitMeasure.setText(ListWeight.getSelectedValue().toString());
            NameSelected=ListWeight.getSelectedValue().toString();
        
    }//GEN-LAST:event_ListWeightMouseClicked

    /**
    * @param args the command line arguments
    */
//
//    public static void main(String args[]) {
//           AddWeightUnitMeasure addWeight=new AddWeightUnitMeasure(null, true);
//         addWeight.setLocationRelativeTo(null);
//         addWeight.setVisible(true);
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList ListWeight;
    private javax.swing.JButton bntAdd;
    private javax.swing.JButton bntCancel;
    private javax.swing.JButton bntDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtWeightUnitMeasure;
    // End of variables declaration//GEN-END:variables
 String NameSelected;
 WeightUnitMeasure weight;
}
